<div>
    <div class="row">
        <div class="col-lg-12">
            <h1 class="page-header">Groups Configuration</h1>
        </div>
    </div>

    <span us-spinner="{radius:30, width:8, length: 16}" spinner-key="group-spinner"></span>
    <div class="row">
        <div class="col-lg-12 col-sm-12">
            <div class="panel panel-default">
                <div class="panel-heading apollo-form-headline">
                    Select Group
                    <button type="button" class="btn btn-success btn-xs pull-right" data-toggle="modal" data-target="#add-edit-group" ng-click="newGroup()">
                        <i class="fa fa-plus"></i> New Group
                    </button>
                </div>
                <div class="panel-body">
                    <div class="table-responsive">
                        <table class="table table-hover" datatable="ng" dt-options="dtOptions">
                            <thead>
                            <tr>
                                <th>Name</th>
                                <th>Service</th>
                                <th>Environment</th>
                                <th>Scaling Factor</th>
                                <th>JSON Parameter</th>
                                <th>Actions</th>
                            </tr>
                            </thead>
                            <tbody>
                            <tr ng-repeat="group in allGroups">
                                <td>{{group.name}}</td>
                                <td>{{allServices[group.serviceId].name || "*"}}</td>
                                <td>{{allEnvironments[group.environmentId].name || "*"}}</td>
                                <td>{{group.scalingFactor}}</td>
                                <td><pre>{{ group.jsonParams }}</pre></td>
                                <td>
                                    <button type="button" class="btn btn-primary btn-circle" uib-tooltip="Edit"
                                            ng-click="setCurrentGroup(group)"
                                            data-toggle="modal" data-target="#add-edit-group">
                                        <i class="fa fa-pencil-square-o"></i>
                                    </button>
                                    <button type="button" class="btn btn-danger btn-circle" uib-tooltip="Delete"
                                            ng-click="setCurrentGroup(group)"
                                            data-toggle="modal" data-target="#delete-group">
                                        <i class="fa fa-trash-o"></i>
                                    </button>
                                    <button type="button" class="btn btn-info btn-circle" uib-tooltip="Change scaling factor"
                                            ng-click="setCurrentGroup(group)"
                                            data-toggle="modal" data-target="#change-scaling-factor">
                                        <i class="fa fa-calculator"></i>
                                    </button>
                                </td>
                            </tr>
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <div class="modal fade" id="add-edit-group" tabindex="-1" role="dialog" aria-hidden="true" style="display: none;">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                    <h4 class="modal-title">Add or edit group</h4>
                </div>
                <div class="modal-body">
                    <form name="groupForm">
                        <h4>General Group Configuration</h4>
                        <div class="row">
                            <div class="col-lg-3 text-center" >
                                <span class="apollo-form-headline">Service</span> <br>
                                <select class="form-control apollo-filter" ng-model="groupService" ng-options="service.name for service in allServices">
                                    <option value="">*</option>
                                </select>
                            </div>
                            <div class="col-lg-3 text-center">
                                <span class="apollo-form-headline">Environment</span> <br>
                                <select class="form-control apollo-filter" ng-model="groupEnvironment" ng-options="environment.name for environment in allEnvironments">
                                    <option value="">*</option>
                                </select>
                            </div>
                            <div class="col-lg-3 text-center">
                                <span class="apollo-form-headline">Name</span> <br>
                                <div class="form control" ng-class="{'has-error': groupForm.groupNameInput.$error.pattern}">
                                    <input class="form-control" type="text" placeholder="group-name" name="groupNameInput"
                                           ng-model="groupName" ng-required="true" ng-pattern="/^[a-zA-Z0-9-_]*$/">
                                </div>
                            </div>
                            <div class="col-lg-3 text-center">
                                <span class="apollo-form-headline">JSON Params</span> <br>
                                <div class="form control">
                                    <input class="form-control" type="text" placeholder="{}" name="groupJsonParamsInput"
                                           ng-model="groupJsonParams" ng-required="false">
                                </div>
                            </div>
                        </div>
                        <br><br>
                    </form>
                    <h6>* New group will get default scaling factor of 1. You can use "Change scaling factor" option afterwards.</h6>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-success" data-dismiss="modal" ng-click="saveGroup()">Save</button>
                    <button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
                </div>
            </div>
        </div>
    </div>

    <div class="modal fade" id="change-scaling-factor" tabindex="-1" role="dialog" aria-hidden="true" style="display: none;">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                    <h4 class="modal-title">Change scaling factor</h4>
                </div>
                <div class="modal-body">
                    <form name="scalingFactorForm">
                        <h4>Choose how many pods of this group you want</h4>
                        <div class="row">
                            <div class="col-lg-3 text-center">
                                <span class="apollo-form-headline">New scaling factor</span> <br>
                                <div class="form control">
                                    <input class="form-control" type="number" min="0" placeholder="{{ groupScalingFactor }}" name="newScalingFactorInput"
                                           ng-model="newScalingFactor" ng-required="true" valid-number>
                                </div>
                            </div>
                        </div>
                        <br><br>
                    </form>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-success" data-dismiss="modal" ng-click="changeScalingFactor()">Scale</button>
                    <button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
                </div>
            </div>
        </div>
    </div>

    <div class="modal fade" id="delete-group" tabindex="-1" role="dialog" aria-hidden="true" style="display: none;">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                    <h4 class="modal-title">Delete group</h4>
                </div>
                <div class="modal-body">
                    <form name="scalingFactorForm">
                        <h4>Are you sure you want to delete {{ groupName }}?</h4>
                        <br><br>
                    </form>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-danger" data-dismiss="modal" ng-click="deleteGroup()">Delete</button>
                    <button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
                </div>
            </div>
        </div>
    </div>
</div>
